package scenebuilders;

import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Group;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Point3d;

import com.sun.j3d.utils.behaviors.mouse.MouseBehaviorCallback;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;

public class BehaviorBuilder {
	MouseBehaviorCallback mbc;
	TransformGroup objTrans;
	boolean isMouseRotate;
	boolean isMouseTranslate;
	boolean isMouseZoom;
	
	public enum BehaviorType {rotate, translate, zoom};
									
	public BehaviorBuilder(MouseBehaviorCallback mbc, Group sceneRoot) {
		this.mbc = mbc;
		isMouseRotate = isMouseTranslate = isMouseZoom = false;
		objTrans = new TransformGroup(new Transform3D());               
		objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	}
	
	public TransformGroup GetBehaviorRoot() {
		return objTrans;
	}
	
	public void SetAll() {
		isMouseRotate = isMouseTranslate = isMouseZoom = true;
	}
	
	public void SetAllExcept(BehaviorType behavior) {
		isMouseRotate = isMouseTranslate = isMouseZoom = true;
		switch(behavior) {
		case translate: isMouseTranslate = false; break;			
		case rotate: isMouseRotate = false; break;
		case zoom: isMouseZoom = false; break;			
		}
	}

	public void AddBehavior(BranchGroup sceneRoot) {
		BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0,
				0.0), 100.0);
		
		if (isMouseRotate || isMouseTranslate || isMouseZoom)
			sceneRoot.addChild(objTrans);
		
		if (isMouseRotate) {
			MouseRotate mouseRotate = new MouseRotate(objTrans);
			mouseRotate.setSchedulingBounds(bounds);
			mouseRotate.setupCallback(mbc);
			sceneRoot.addChild(mouseRotate);
		}

		if (isMouseTranslate) {
			MouseTranslate mouseTranslate = new MouseTranslate(objTrans);
			mouseTranslate.setSchedulingBounds(bounds);
			mouseTranslate.setupCallback(mbc);
			sceneRoot.addChild(mouseTranslate);
		}

		if (isMouseZoom) {
			MouseZoom mouseZoom = new MouseZoom(objTrans);
			mouseZoom.setSchedulingBounds(bounds);
			mouseZoom.setupCallback(mbc);
			sceneRoot.addChild(mouseZoom);
		}
	}
}